using System;
using Unity.Burst;
using Unity.Entities;
using UnityEngine;
using UnityEngine.UI;

namespace HelloCube.GameObjectSync
{
    public partial struct DirectoryInitSystem : ISystem
    {
        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            //初始化前需等待场景加载该对象完毕
            state.RequireForUpdate<Execute.GameObjectSync>();
        }

        //方法内部使用了托管对象时不可使用 BurstCompile
        //[BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            state.Enabled = false;

            var go = GameObject.Find("Directory");
            if (go == null)
            {
                throw new Exception("GameObject 'Directory' not found.");
            }

            var directory = go.GetComponent<Directory>();
            var directoryManaged = new DirectoryManaged();
            directoryManaged.RotatorPrefab = directory.RotatorPrefab;
            directoryManaged.RotationToggle = directory.RotationToggle;

            var entity = state.EntityManager.CreateEntity();
            state.EntityManager.AddComponentData(entity, directoryManaged);
        }
    }

    public class DirectoryManaged : IComponentData
    {
        public GameObject RotatorPrefab;
        public Toggle RotationToggle;
        
        //托管类型的 组件 必须有一个无参的构造函数，否则报错
        public DirectoryManaged(){}
    }
}